var api = require('../../utils/util.js').default;
var app = getApp();
var index = 0; //便利的索引值
Page({
  data: {
    confirmBoxVisible: false, //确认弹框的显示
    deliverList: [],
    currentDeliverList: {},
    adv: "",
  },

  // 进入订单提交页面
  cus_post_list: function () {
    if (!app.globalData.is_visitor) {
      wx.navigateTo({
        url: '../square_cuspush/square_cuspush',
      })
    } else {
      wx.showToast({
        title: '请登录',
        duration: 700,
        icon: "loading"
      })
    }
  },

  // 获取广告头接口
  get_adv: function () {
    var data = {
    }
    console.log("获取广告")
    api.adv(data).then(res=>{
      console.log("获取广告")
        console.log(res.data)
        if (res.data.data.url.length < 1) {
          this.setData({
            adv: "../../ico/timg.jpg"
          })
        } else {
          this.setData({
            adv: res.data.data.url
          })
        }
    })
  },

  // 接单弹窗按钮
  chooseOrder: function (event) {
      index = event.currentTarget.dataset.index;
      console.log(index);
      this.setData({
        currentDeliverList: this.data.deliverList[index]
      });
      console.log("当前的deliverList是");
      console.log(this.data.currentDeliverList)
    if(app.globalData.is_visitor){
      wx.showToast({
        title: '请登录',
        icon: "loading",
        duration: 700
      })
    }else if(!app.globalData.is_defined){
      wx.showToast({
        title: '请先实名认证',
        icon: "loading",
        duration: 700
      })
    }else if(this.data.currentDeliverList.tocustomer==app.globalData.openid){
      wx.showToast({
        title: '自己的单哦~',
        icon: "loading",
        duration: 700
      })
    }else{
      this.setData({
        confirmBoxVisible: true,
      })
    }
  },
  // 接单弹窗关闭
  close: function () {
    this.setData({
      confirmBoxVisible: false,
    })
  },

  // 接单成功上传接口
  riderConfirmOrder: function (event) {
    let deliverListItem = "deliverList[" + index + "].is_matched";
    this.setData({
      [deliverListItem]: "1",
    });
    var data = {
      torider: app.globalData.openid, //骑手名
      status: 1, //状态为1 已被接单
      id: this.data.currentDeliverList.shoppingId, //订单id
    }
    api.riderConfirmOrder(data).then(res=>{
      console.log(res.data)
        wx.showToast({
          title: '接单成功！',
          icon: 'success',
          duration: 1500
        })
        this.getUnmatchedOrder();
    })
    this.close();
  },

  // 可接单列表加载
  getUnmatchedOrder: function () {
    this.setData({
      deliverList: []
    })
    var data = {
    }
    api.getUnmatchedOrder(data).then(result=>{
      var arr = result.data;
      console.log("首页接单列表：");
      console.log(arr);
      var dataList = new Array();
      dataList = arr.data; 
      console.log(dataList);
      for (let i = 0; i < dataList.length; i++) {
        var singleData = new Array();
        if (dataList[i]) {
          singleData = dataList[i];
          console.log(singleData);
          switch (singleData.sever_type) {
            case "1":
              singleData.sever_type = "快递代拿";
              break;
            case "2":
              singleData.sever_type = "外卖代拿";
              break;
            case "3":
              singleData.sever_type = "食堂代拿";
              break;
            case "4":
              singleData.sever_type = "超市代买";
              break;
            default:
              singleData.sever_type = "无效服务";
          }
          let singleObject = {};
          singleObject = {
            tocustomer: singleData.tocustomer, //客户名
            sever_type: singleData.sever_type, //服务类型选择
            meet_place: singleData.meet_place, //交接地点
            unit_price: singleData.unit_price, //单价
            cus_details: singleData.cus_details, //客户的备注信息
            upload_time: singleData.upload_time, //订单提交时间
            choose_rider: singleData.torider, //选择骑手
            is_matched: singleData.is_matched, //是否匹配到骑手
            on_working: singleData.on_working, //订单是否被配送
            is_outdata: singleData.is_outdata, //订单是否完成
            is_failed: singleData.is_failed, //订单是否被遗弃
            shoppingId: singleData.ID, //订单编号,
            img_url:singleData.head,
          }
          this.setData({
            deliverList: this.data.deliverList.concat(singleObject)
          });
        }
      }
      console.log(this.data.deliverList);
    })
  },

  onShow: function (options) {
    this.get_adv()
    this.getUnmatchedOrder()
  },

   // 下拉刷新组件
   onPullDownRefresh: function () {
    this.getUnmatchedOrder()
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 300);
  },

})